﻿using NPOI.SS.Formula.Functions;
using Prism.Events;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ZoneLinePacking.Core.EventArgs;
using ZoneLinePacking.Services.Message;
using ZoneLinePacking.ViewModels;

namespace ZoneLinePacking.Views {
    /// <summary>
    /// MainPackingView.xaml 的交互逻辑
    /// </summary>
    public partial class MainPackingView : UserControl {

        private readonly IMessageService _messageBox;
        private readonly IEventAggregator _eventAggregator;
        public MainPackingView(
            IEventAggregator eventAggregator, 
            IMessageService messageBox
            ) {
            
            InitializeComponent();
            this._eventAggregator = eventAggregator;
            this._messageBox = messageBox;

            _eventAggregator.GetEvent<PubSubEventArgs>().Subscribe(OnEventReceived);

        }

        private void OnEventReceived(MessageData data) {

            if (data.Code == nameof(MainPackingViewModel)) {
                App.Current.Dispatcher.Invoke(() =>
                {
                    if (data.Parameters.ContainsKey(MainPackingViewModel._scanBarCodeKey) && data.Parameters.ContainsKey(MainPackingViewModel._scanCountKey))
                    {
                        string scanBarCode = data.Parameters.GetValue<string>(MainPackingViewModel._scanBarCodeKey);
                        int scanCount = data.Parameters.GetValue<int>(MainPackingViewModel._scanCountKey);
                        string str = $"扫码枪收到第{scanCount}个条码：{scanBarCode}";
                        AppendLogMessage(str);
                    }

                    if (data.Parameters.ContainsKey(MainPackingViewModel._successKey))
                    {// 成功
                        var message = data.Parameters.GetValue<string>(MainPackingViewModel._successKey);
                        packageRltTxtMsg.Background = new SolidColorBrush(Colors.Green);
                        packageRltTxtMsg.Text = message;
                        AppendLogMessage(message);
                    }
                    else if (data.Parameters.ContainsKey(MainPackingViewModel._errorKey))
                    {// 失败
                        string message = data.Parameters.GetValue<string>(MainPackingViewModel._errorKey);
                        packageRltTxtMsg.Background = new SolidColorBrush(Colors.Red);
                        packageRltTxtMsg.Text = message;
                        var res = _messageBox.Error(message, "装箱错误");
                        if (res == MessageBoxResult.OK)
                        {
                            var dataContext = (this.DataContext as MainPackingViewModel);
                            dataContext.InAlarmState = false;
                        }
                    }
                });
            }

        }


        private int _logCount = 0;
        private void AppendLogMessage(string info) {
            if (info is string ss) {
                Dispatcher.Invoke(() => {
                    _logCount++;
                    if (_logCount > 500) {
                        packageBoxLog.Document.Blocks.Clear();
                        _logCount = 0;
                    }
                    packageBoxLog.AppendText(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "=>" + ss + "\r\n");
                    packageBoxLog.ScrollToEnd();
                });
            }
        }


    }
}
